package com.sgb.kjwl.view.ui.activity.lease;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLeaseHall2Binding;
import com.sgb.kjwl.view.ui.activity.house.LeaseFactoryListFragment;
import com.sgb.kjwl.view.ui.activity.house.LeaseFarmListFragment;
import com.sgb.kjwl.view.ui.activity.house.LeaseHouseListFragment;
import com.sgb.kjwl.view.ui.activity.house.LeaseLandListFragment;
import com.sgb.kjwl.view.ui.fragment.device.DeviceListFragment3;
import com.sgb.kjwl.view.ui.fragment.machine.CarListFragment3;
import com.sgb.kjwl.view.ui.fragment.machine.MachineListFragment3;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;

import java.util.ArrayList;

/**
 * 出租大厅
 */
public class LeaseHallFragment2 extends AppLazyFragment {
    private FragmentLeaseHall2Binding binding;
    private FragPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    private final String[] titles = {"全部", "机械租赁", "设备租赁", "车辆租赁", "房产租赁", "土地租赁", "工厂租赁", "农机租赁"};
    private int categoryType = 0;
    private int categoryFirst;
    private int categorySecond;
    private String isSelf;

//    public static void start(Context context) {
//        Intent intent = new Intent();
//        intent.setClass(context, LeaseHallFragment.class);
//        context.startActivity(intent);
//    }
//
//    public static void start(Context context, int type, int categoryFirst, int categorySecond) {
//        Intent intent = new Intent();
//        intent.putExtra("type", type);
//        intent.putExtra("categoryFirst", categoryFirst);
//        intent.putExtra("categorySecond", categorySecond);
//        intent.setClass(context, LeaseHallFragment.class);
//        context.startActivity(intent);
//    }
//
//    public static void start(Context context, String isSelf) {
//        Intent intent = new Intent();
//        intent.putExtra("isSelf", isSelf);
//        intent.setClass(context, LeaseHallFragment.class);
//        context.startActivity(intent);
//    }

    public static LeaseHallFragment2 newInstance(String isSelf) {
        LeaseHallFragment2 fragment = new LeaseHallFragment2();
        Bundle bundle = new Bundle();
        bundle.putString("isSelf", isSelf);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_lease_hall2, container, false);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
//        binding.ivBack.setVisibility(View.VISIBLE);
//        binding.ivBack.setOnClickListener(this);
//        binding.tvRelease.setOnClickListener(this);

//        binding.editSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
//        binding.editSearch.setOnKeyListener(new View.OnKeyListener() {
//            @Override
//            public boolean onKey(View v, int keyCode, KeyEvent event) {
//                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//                    closeKeybord();
//                    EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_HOUSE_FILTER).setMessageData(binding.editSearch.getText().toString()));
//                }
//                return false;
//            }
//        });
    }

    @Override
    protected void onLazyLoad() {
        init();
    }

    private void init() {
        if (getArguments() != null) {
            isSelf = getArguments().getString("isSelf");
        }
        mFragments = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            if ("全部".equals(titles[i])) {
                mFragments.add(MachineListFragment3.newInstance("", isSelf));
            } else if ("机械租赁".equals(titles[i])) {
                mFragments.add(MachineListFragment3.newInstance("1", isSelf));
            } else if ("设备租赁".equals(titles[i])) {
                mFragments.add(DeviceListFragment3.newInstance(isSelf));
            } else if ("车辆租赁".equals(titles[i])) {
                mFragments.add(CarListFragment3.newInstance(isSelf));
            } else if ("房产租赁".equals(titles[i])) {
                mFragments.add(LeaseHouseListFragment.newInstance(isSelf));
            } else if ("土地租赁".equals(titles[i])) {
                mFragments.add(LeaseLandListFragment.newInstance(isSelf));
            } else if ("工厂租赁".equals(titles[i])) {
                mFragments.add(LeaseFactoryListFragment.newInstance(isSelf));
            } else if ("农机租赁".equals(titles[i])) {
                mFragments.add(LeaseFarmListFragment.newInstance(isSelf));
            }
        }

        initExtra();
        pagerAdapter = new FragPagerAdapter(getChildFragmentManager(), mFragments);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(1);
        binding.tablayout.setViewPager(binding.viewpager, titles);
        binding.tablayout.setCurrentTab(categoryType);

    }

    //给Fragment设置从外部传递的属性
    private void initExtra() {
        if (getArguments() != null) {
            categoryType = getArguments().getInt("categoryType", 0);
//            categoryFirst = getArguments().getInt("categoryFirst", 0);
//            categorySecond = getArguments().getInt("categorySecond", 0);
//            // 因第0个下标数据为"不限,全部",则下标需+1才能和分类界面对应
//            Bundle bundle = getArguments();
//            bundle.putInt("categoryFirst", categoryFirst);
//            bundle.putInt("categorySecond", categorySecond);
//            mFragments.get(categoryType).setArguments(bundle);
        }
    }


}
